﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightGolf.Core
{
    public struct Vector2
    {
        public double X;
        public double Y;

        public double Length
        {
            get { return (X.Pow(2) + Y.Pow(2)).Sqrt(); }
        }

        public double LengthSquared
        {
            get { return X.Pow(2) + Y.Pow(2); }
        }

        public Vector2 Normal
        {
            get
            {
                return this / this.Length;
            }
        }

        public static Vector2 Zero
        {
            get { return new Vector2(0, 0); }
        }

        public static Vector2 UnitX
        {
            get { return new Vector2(1, 0); }
        }

        public static Vector2 UnitY
        {
            get { return new Vector2(0, 1); }
        }

        public Vector2(double value)
        {
            this.X = value;
            this.Y = value;
        }

        public Vector2(double X, double Y)
        {
            this.X = X;
            this.Y = Y;
        }

        public static Vector2 operator +(Vector2 V1, Vector2 V2)
        {
            return new Vector2(V1.X + V2.X, V1.Y + V2.Y);
        }

        public static Vector2 operator -(Vector2 V1)
        {
            return new Vector2(-V1.X, -V1.Y);
        }

        public static Vector2 operator -(Vector2 V1, Vector2 V2)
        {
            return new Vector2(V1.X - V2.X, V1.Y - V2.Y);
        }

        public static Vector2 operator *(Vector2 V1, Vector2 V2)
        {
            return new Vector2(V1.X * V2.X, V1.Y * V2.Y);
        }

        public static Vector2 operator *(Vector2 V, double multiplier)
        {
            return new Vector2(V.X * multiplier, V.Y * multiplier);
        }

        public static Vector2 operator /(Vector2 V1, Vector2 V2)
        {
            return new Vector2(V1.X / V2.X, V1.Y / V2.Y);
        }

        public static Vector2 operator /(Vector2 V, double multiplier)
        {
            return new Vector2(V.X / multiplier, V.Y / multiplier);
        }

        public Vector2 Rotate(double radians)
        {
            return new Vector2(this.X * Math.Cos(radians) - this.Y * Math.Sin(radians), this.X * Math.Sin(radians) + this.Y * Math.Cos(radians));
        }

        public Vector2 ReduceLength(double value)
        {
            if (value > this.Length)
                return Vector2.Zero;

            return this - this.Normal * value;
        }

        public override string ToString()
        {
            return "{{{0:0.000}:{1:0.000}}}".Format(X, Y);
        }
    }
}
